package com.project.bookstore.book.controller;

import com.project.bookstore.book.entity.Banner;
import com.project.bookstore.book.mapper.BannerMapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;
@Api(tags="Banner")
@RestController
public class BannerController {
    @Autowired(required = false)
    BannerMapper mapper;

    @GetMapping("/indexAllBanner")
    public List<Banner> indexAllBanner() {
        return mapper.bannerAll();
    }

    @PostMapping("/insertBanner")
    public void insertBanner(Banner banner) {
        mapper.insertBanner(banner);
    }

    @RequestMapping("/banner/delete")
    public void delete(int id) {
        //得到删除轮播图的图片名     /xxxx.jpg
        String url = mapper.selectUrlById(id);
        //得到文件的完整路径
        String filePath = "D:/file" + url;
        //删除文件
        new File(filePath).delete();
        mapper.deleteById(id);
    }
}
